package com.eattravel.service.impl;

import com.eattravel.mapper.CommodityMapper;
import com.eattravel.mapper.OrderCommodityMapper;
import com.eattravel.pojo.Commodity;
import com.eattravel.pojo.OrderCommodity;
import com.eattravel.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService   {

    @Autowired
    CommodityMapper commodityMapper;

    @Autowired
    OrderCommodityMapper orderCommodityMapper;

    @Override
    public Commodity findCommById(String commodityId) {


        Commodity commodity= commodityMapper.findCommById(commodityId);
        if(null!=commodity){
            return commodity;
        }
        return null;
    }

    /**
     * 综合排序
     * @param area
     * @return
     */
    @Override
    public List<Commodity> getFoodByArea(String area) {
        List<Commodity> commodities = commodityMapper.selectAllByArea(area);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }

    /**
     * 评分排序
     * @param area
     * @return
     */
    @Override
    public List<Commodity> getFoodByArea2(String area) {
        List<Commodity> commodities = commodityMapper.selectAllByArea02(area);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }

    /**
     * 销量排序
     * @param area
     * @return
     */
    @Override
    public List<Commodity> getFoodByArea3(String area) {
        List<Commodity> commodities = commodityMapper.selectAllByArea03(area);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }

    /**
     * 价格排序
     * @param area
     * @return
     */
    @Override
    public List<Commodity> getFoodByArea4(String area) {
        List<Commodity> commodities = commodityMapper.selectAllByArea04(area);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }

    /**
     * 标签查找结果获取的，四种排序
     * @param label
     * @return
     */
    @Override
    public List<Commodity> getFoodByLabel1(String label) {
        List<Commodity> commodities = commodityMapper.selectAllByLabel1(label);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }
    @Override
    public List<Commodity> getFoodByLabel2(String label) {
        List<Commodity> commodities = commodityMapper.selectAllByLabel2(label);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }
    @Override
    public List<Commodity> getFoodByLabel3(String label) {
        List<Commodity> commodities = commodityMapper.selectAllByLabel3(label);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }
    @Override
    public List<Commodity> getFoodByLabel4(String label) {
        List<Commodity> commodities = commodityMapper.selectAllByLabel4(label);
        if (commodities.size() != 0){
            return commodities;
        }
        return null;
    }

    @Override
    public List<Commodity> findCommodityByOrderId(String orderId) {
        List<Commodity> commodityByOrderId = commodityMapper.findCommodityByOrderId(orderId);

        return commodityByOrderId;
    }

    @Override
    public List<OrderCommodity> findCommodityByOrderId1(String orderId) {
     List<OrderCommodity> commodityByOrderId= orderCommodityMapper.findCommodityByOrderId1(orderId);
     return commodityByOrderId;
    }

    @Override
    public int updataComm(String commodityId, float commGrade) {
        Commodity commodity= commodityMapper.findCommById(commodityId);
       int  num=commodity.getCommodityNum();

        float score=commodity.getCommodityScore().floatValue();

        float newScore=(num*score+commGrade)/(num+1);
        float newScore2=(float)(Math.round(newScore*100))/100;
        int count=commodityMapper.updataComm(commodityId,newScore2);
        System.out.println(count);

        return 0;
    }




}
